<script>
    // function main(arr) {
    //     let max = 0
    //     for (let i = 0; i < arr.length - 1; i++) {
    //         for (let j = i + 1; j < arr.length; j++) {
    //             // ...codes
    //             let area = getArea(i, j)
    //             max = Math.max(max, area)
    //         }
    //     }
    //     return max
    // }

    // function getArea(i, j) {
    //     return Math.min(arr[i], arr[j]) * (j - i)
    // }

    function main(arr) {
        let left = 0
        let right = arr.length - 1
        let max = 0
        while (left < right) {
            let area = Math.min(arr[left], arr[right]) * (right - left)
            if (max < area) {
                max = area
            }
            if (arr[left] < arr[right]) {
                left += 1
            } else {
                right -= 1
            }
        }
        return max
    }

    let arr = [1, 8, 4, 7, 6]

    console.log(main(arr))
</script>